@extends('admin.layouts.app')
@section('headerCss')
    @parent
    <style>
        .w88 {
            width: 88px!important;
        }
    </style>
@stop
@section('content')
    <div id="add_ads_style">
        {!! Form::open(['route' => 'coupon.store', 'method' => 'post' ,'id' =>'form']) !!}

        <div class="add_adverts">
            <ul>
                <li>
                    <label class="label_name">所属券类型</label>

                    <span class="cont_style">
						<select class="form-control" id="form-field-select-1" name="type">
							<option value="">选择券类型</option>
							@foreach ($types as $type)
                                <option min_amount="{{$type->min_amount}}" value="{{ $type->id }}">{{ $type->name }}</option>
							@endforeach
						</select></span>
                </li>
                <li><label class="label_name w88">优惠券名称</label><span class="cont_style">
                        <input name="name"  type="text"
                               id="name" placeholder="" class="col-xs-10 col-sm-5" style="width:150px"></span>
                </li>
                <li><label  class="label_name w88">最低消费金额</label><span class="cont_style">
                        <input name="min_amount" type="text"
                               id="min_amount" placeholder="" class="col-xs-10 col-sm-5" style="width:90px"></span>
                </li>
                <li>
                    <label class="label_name w88">开始时间</label>
                    <input class="laydate-icon" name="not_before" id="start" style="width:200px; margin-left:10px; padding-left:5px; margin-right:10px; display:block; float:left" />
                </li>
                <li>
                    <label class="label_name w88">过期时间</label>
                    <input class="laydate-icon" name="not_after" id="end" style="width:200px; margin-left:10px; padding-left:5px; margin-right:10px; display:block; float:left" />
                </li>
                <li><label class="label_name w88">生成数量</label>
                    <span class="cont_style">
                        <input name="num" type="text" id="form-field-1" placeholder="0"
                               class="col-xs-10 col-sm-5" style="width:50px"></span>
                </li>
                <li>
                    <div class="Button_operation">
                        <button type class="btn btn-primary radius" type="submit">保存并提交</button type>
                        <button onclick="layer_close()" class="btn btn-default radius" type="button">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
                    </div>
                </li>
            </ul>
        </div>
        {!! Form::close() !!}
    </div>

@stop
@section('js')
<script src="{{ asset('admin/assets/laydate/laydate.js') }}" type="text/javascript"></script>
<script>
    var start = {
        elem: '#start',
        format: 'YYYY-MM-DD',
        min: laydate.now(), //设定最小日期为当前日期
        max: '2099-06-16', //最大日期
        istime: true,
        istoday: false,
        choose: function(datas){
            end.min = datas; //开始日选好后，重置结束日的最小日期
            end.start = datas //将结束日的初始值设定为开始日
        }
    };
    var end = {
        elem: '#end',
        format: 'YYYY-MM-DD',
        min: laydate.now(),
        max: '2099-06-16 ',
        istime: true,
        istoday: false,
        choose: function(datas){
            start.max = datas; //结束日选好后，重置开始日的最大日期
        },
        done: function(value, date, endDate){
            console.log(value); //得到日期生成的值，如：2017-08-18
            console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
            console.log(endDate); //得结束的日期时间对象，开启范围选择（range: true）才会返回。对象成员同上。
        }
    };
    laydate(start);
    laydate(end);
</script>

<script type="text/javascript">
    $('#form').Validform({
        tiptype : 4,
        ajaxPost : true,
        beforeCheck:function(curform){
            console.log(curform);
            //在表单提交执行验证之前执行的函数，curform参数是当前表单对象。
            //这里明确return false的话将不会继续执行验证操作;
            index = layer.load(1, {shade: [0.6, '#393D49']});
        },
        callback : function (res) {
            console.log(res);
            if(res.status == 422) {
                var json=JSON.parse(res.responseText);
                $.each(json.errors, function(idx, obj) {
                    layer.msg(obj[0], {icon: 5});
                    layer.close(index);
                    return false;
                });
            } else if(res.status== 200) {
                layer.msg(res.msg, {
                    icon: 1,
                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                }, function(){
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    parent.location.href = '{{ route('coupon.index') }}'
                });

            }
        },
    });
</script>
<script>
    $(function () {
        $('#name').attr("readonly",true);
        $('#min_amount').attr("readonly",true);
        $('#form-field-select-1').bind('change',function () {
            $('#name').val($("#form-field-select-1 :selected").text())
            //min_amount
            $('#min_amount').val($("#form-field-select-1 :selected").attr('min_amount'))
        })
    })
</script>
@stop
